package com.apes.scm.masterdata.party.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.party.model.*;
import com.apes.scm.masterdata.party.repository.PartyRepository;
import com.apes.scm.masterdata.party.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 功能：内部员工信息
 *
 * @author HuJianlin
 * @create 2018-03-17 16:35
 */
@Service("employeeService")
public class EmployeeService extends DomainService {
    private static final String ROLE_TYPE = "Employee";

    @Autowired
    private PartyService partyService;

    @Autowired
    private PartyRepository partyRepository;

    @Autowired
    PartyRoleService partyRoleService;

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private SequenceService sequenceService;

    public Party save(SimpleRequest request) {
        Party party = request.getO(Party.class);
        party.setPartyType("person");

        // 保存角色
        Set<PartyRole> partyRoleSet = new HashSet<>();
        PartyRole partyRole = new PartyRole();
        partyRole.setParty(party);

        String roleId = sequenceService.getId("PARTY_Employee");
        RoleType roleType = new RoleType();
        roleType.setId("Employee");
        partyRole.setRole(roleType);
        partyRole.setId(roleId);
        partyRole.setParty(party);
        partyRoleSet.add(partyRole);
        party.setPartyRoles(partyRoleSet);


        // 保存人员标识信息
        Set<PartyIdentification> partyIdentificationSet = new HashSet<>();
        PartyIdentification partyIdentification = new PartyIdentification();
        partyIdentification.setPartyIdType("EmployeeCode");
        partyIdentification.setIdValue(roleId);
        partyIdentification.setParty(party);
        partyIdentificationSet.add(partyIdentification);
        party.setPartyIdentifications(partyIdentificationSet);

        partyIdentification = new PartyIdentification();
        partyIdentification.setPartyIdType("PIT_mobile");
        partyIdentification.setIdValue(party.getPerson().getMobileNumber());
        partyIdentification.setParty(party);
        partyIdentificationSet.add(partyIdentification);
        party.setPartyIdentifications(partyIdentificationSet);


        return partyRepository.saveAndFlush(party);
    }

    public Party update(SimpleRequest request) {
        Party party = request.getO(Party.class);
        party.setUpdateDate(new Date());

        List<Map> condition = new ArrayList<>();

        JSONObject mainCondition = new JSONObject();
        mainCondition.put("field", "person.id");
        mainCondition.put("operator", "EQ");
        mainCondition.put("value", party.getPerson().getId());
        condition.add(JSONObject.toJavaObject(mainCondition, Map.class));
        List<Party> list = partyRepository.findAll(JpaDsl.toCriteria(condition));
//        System.out.println(list);

        party.setPartyIdentifications(list.get(0).getPartyIdentifications());
        party.setPartyRoles(list.get(0).getPartyRoles());

        return partyRepository.saveAndFlush(party);
    }

    public Page<Party> findAll(SimpleRequest request) {
        List<Map> condition = request.get("condition");
        return partyRepository.findAll(JpaDsl.toCriteria(condition), PageRequest.of(request.get("page"), request.get("size")));
    }

    public String addEmployeeRole(SimpleRequest simpleRequest) {
        return partyRoleService.addPartyTypeAndRoleId(simpleRequest,"person","Employee");
    }

    public Party findOne(long id) {
        return partyService.findOne(id);
    }

    /**
     * 根据Mobile查找人员信息
     * @param request
     * @returnmobilePhone
     */
    public Party findByMobile(SimpleRequest request) {
        return partyService.findByMobile(request);
    }

    /**
     * 根据Code查找人员信息
     * @param request
     * @return
     */
    public Party findByCode(SimpleRequest request) {
        return partyService.findByCode(request, ROLE_TYPE + "Code");
    }


    /**
     * 查找人员身份证
     * @param request
     * @return
     */
    public Person findPersonIdCard(SimpleRequest request) {
        return personRepository.findPersonIdCard(request.get("idCard"));
    }

    /**
     * 查找人员电话号码
     * @param request
     * @return
     */
    public Person findPersonMobile(SimpleRequest request) {
        return personRepository.findPersonMobile(request.get("mobileNumber"));
    }

}
